package com.linkage.usercenter.controller;

import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.core.util.StrUtil;
import com.linkage.common.base.RequestInfo;
import com.linkage.common.base.ResponseInfo;
import com.linkage.common.constant.CommonSysConstant;
import com.linkage.common.util.JSONCustomUtil;
import com.linkage.usercenter.config.TokenCheckException;
import com.linkage.usercenter.feign.HomeManageFeign;
import com.linkage.usercenter.pojo.vo.*;
import com.linkage.usercenter.service.IManageService;
import com.linkage.usercenter.service.IuserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/exter")
public class ExterUserController {

    private static final Logger logger = LoggerFactory.getLogger(ExterUserController.class);
    @Autowired
    private IuserService userService;
    @Autowired
    private HomeManageFeign homeManageFeign;
    @Autowired
    private IManageService manageService;

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public String test(@RequestBody RequestInfo<String> query){
        String param = query.getBuss();
        System.out.println("----"+param);
        if(StrUtil.equals("1", param)){
            throw new TokenCheckException();
        }else  if(StrUtil.equals("2", param)){
            throw new ValidateException("测试 22222");
        }else  if(StrUtil.equals("3", param)){
            throw new RuntimeException("测试 33333");
        }
        String ss = null;
        try {
            ss = homeManageFeign.test("13454565545605");
            logger.info("第三方返回--" + ss);
        }catch (Exception e){
            return JSONCustomUtil.toJsonStr("success");
        }

        return JSONCustomUtil.toJsonStr(ss);
    }

    /* *
     * @describe:  类型描述 注册 app 注册保安 群众 警察
     * @author shaofeng.cheng
     * @title register
     * @date 2020/4/19 22:55
     * @param [query]
     * @return java.lang.String
     * @version: V1.0
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(@RequestBody RequestInfo<RegisterQUERY> query) {
        RegisterVO vo = new  RegisterVO();
        userService.register(query, vo);
        if(CommonSysConstant.PERSON_CLASS_ONE.equals(query.getBuss().getPersonClass())){
            //更新保安手机归属信息
            if(StrUtil.isNotBlank(query.getBuss().getUserMobile())){
                manageService.mobileTransfer(Arrays.asList(query.getBuss().getUserMobile()));
            }
        }
        return ResponseInfo.success(vo).toString();
    }

    /* *
     * @describe:  类型描述 修改用户信息
     * @author shaofeng.cheng
     * @title updateUserInfo
     * @date 2020/5/10 16:41
     * @param [query]
     * @return java.lang.String
     * @version: V1.0
     */
    @RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
    public String updateUserInfo(@RequestBody RequestInfo<UpdateUserInfoQUERY> query) {
        UpdateUserInfoVO vo = new  UpdateUserInfoVO();
        userService.updateUserInfo(query, vo);
        return ResponseInfo.success(vo).toString();
    }


    /*  *
     * @describe:  类型描述 获取用户信息
     * @author shaofeng.cheng
     * @title
     * @date 2020/4/20 16:13
     * @param
     * @return
     * @version: V1.0
     */
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.POST)
    public String getUserInfo(@RequestBody RequestInfo<UserInfoQUERY> query) {
        UserInfoVO vo = new UserInfoVO();
        userService.getUserInfo(query, vo);
        return ResponseInfo.success(vo).toString();
    }

    /*   *
     * @describe:  类型描述 修改用户密码
     * @author shaofeng.cheng
     * @title
     * @date 2020/4/20 22:53
     * @param
     * @return
     * @version: V1.0
     */
    @RequestMapping(value = "/changePassword", method = RequestMethod.POST)
    public String changePassword(@RequestBody RequestInfo<ChangePasswordQUERY> query) {
        ChangePasswordVO vo = new  ChangePasswordVO();
        userService.changePassword(query, vo);
        return ResponseInfo.success(vo).toString();
    }


    /*   *
     * @describe:  类型描述 用户登录
     * @author shaofeng.cheng
     * @title
     * @date 2020/4/20 22:53
     * @param
     * @return
     * @version: V1.0
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@RequestBody RequestInfo<ExterLoginQUERY> query) {
        ExterLoginVO vo = new  ExterLoginVO();
        vo.setFrom("0");
        userService.login(query, vo);
        return ResponseInfo.success(vo).toString();
    }

    @PostMapping("/forgetPwd")
    public String forgetPwd(@RequestBody RequestInfo<ChangePasswordQUERY> param){
        if(StrUtil.hasBlank(param.getBuss().getPhone(),param.getBuss().getUserPassword(),param.getBuss().getVerifiCode())){
            return ResponseInfo.error("参数校验为空，请查看入参数据").toString();
        }
        ResponseInfo responseInfo = userService.forgetPwd(param.getBuss());
        return responseInfo.toString();
    }
}
